package GUI.Ventas.Facturacion;

import java.rmi.RemoteException;
import java.util.ArrayList;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import sale.entities.FacturaVO;
import sale.entities.SaleVO;
import sale.exceptions.EmptySaleExcpetion;
import service.ServiceFacade;
import service.article.exceptions.ProxyException;
import service.sale.interfaces.SaleProxyMgt;

import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Label;

import GUI.Administracion.Exceptions.ExcepcionesMenuVentas;

public class FacturasDelivery1 extends Dialog {

	protected Object result;
	protected Shell shell;
	ArrayList<SaleVO> ventas;
	ArrayList <FacturaVO> facts = new ArrayList<FacturaVO>();
	private Table table;
	Label error;
	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public FacturasDelivery1(Shell parent, int style, ArrayList<SaleVO> v) {
		super(parent, style);
		setText("SWT Dialog");
		ventas=v;
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		System.out.println(ventas.size());
		SaleProxyMgt uso = ServiceFacade.getInstance().getSaleProxyMgt();
		for(int i=0; i<ventas.size();i++){
		try {
			facts.add(uso.close(ventas.get(i)));
		} catch (RemoteException e) {
			ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
			ventana.open();
			shell.dispose();
			e.printStackTrace();
		} catch (ProxyException e) {
			ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
			ventana.open();
			shell.dispose();
			e.printStackTrace();
		} catch (EmptySaleExcpetion e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		}
		
		
		shell = new Shell(getParent(), getStyle());
		shell.setSize(674, 358);
		shell.setText(getText());
		shell.setLayout(null);
		
		table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
		table.setBounds(10, 29, 654, 175);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		TableColumn tblclmnClaveDeVent = new TableColumn(table, SWT.NONE);
		tblclmnClaveDeVent.setWidth(100);
		tblclmnClaveDeVent.setText("Clave de Venta");
		
		TableColumn tblclmnDireccion = new TableColumn(table, SWT.NONE);
		tblclmnDireccion.setWidth(100);
		tblclmnDireccion.setText("Direccion");
		
		TableColumn tblclmnTelefono = new TableColumn(table, SWT.NONE);
		tblclmnTelefono.setWidth(67);
		tblclmnTelefono.setText("Telefono");
		
		TableColumn tblclmnCliente = new TableColumn(table, SWT.NONE);
		tblclmnCliente.setWidth(67);
		tblclmnCliente.setText("Cliente");
		
		TableColumn tblclmnImporteConsumido = new TableColumn(table, SWT.NONE);
		tblclmnImporteConsumido.setWidth(111);
		tblclmnImporteConsumido.setText("Importe Consumido");
		
		TableColumn tblclmnDescuento = new TableColumn(table, SWT.NONE);
		tblclmnDescuento.setWidth(100);
		tblclmnDescuento.setText("Descuento");
		
		TableColumn tblclmnImporteAPagar = new TableColumn(table, SWT.NONE);
		tblclmnImporteAPagar.setWidth(100);
		tblclmnImporteAPagar.setText("Importe a Pagar");
		
		for(int k=0;k<facts.size();k++){
			TableItem tableItem = new TableItem(table, SWT.NONE);
			tableItem.setText(0,String.valueOf(facts.get(k).getPrimaryKey()));
			tableItem.setText(1,facts.get(k).getSale().getAddress());
			tableItem.setText(2,facts.get(k).getSale().getTel());
			if(facts.get(k).getClient()==null){
				tableItem.setText(3,"No especificado");
			}
			else{
				tableItem.setText(3,facts.get(k).getClient().getName() + facts.get(k).getClient().getLastName());
				
			}
			tableItem.setText(4,String.valueOf(facts.get(k).getTotal()));
			tableItem.setText(5,String.valueOf(facts.get(k).getDescuento()));
			tableItem.setText(6,String.valueOf(facts.get(k).getTotaltotalConDesc()));
		}
		
		Button btnVerDetalleE = new Button(shell, SWT.NONE);
		btnVerDetalleE.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(table.getSelection()[0]==null){
					error.setText("Seleccione la Venta de la que Quiere Ver la Factura");
				}
				TableItem seleccionado = table.getSelection()[0];
				FacturaVO fact = null;
				for(int i=0;i<facts.size();i++){
					if(Integer.parseInt(seleccionado.getText(0))==facts.get(i).getPrimaryKey()){
						fact=facts.get(i);
					}
				}
				CerrarVenta cerrar = new CerrarVenta (shell, shell.getStyle(), null, fact);
				shell.setVisible(false);
				try {
					cerrar.open();
				} catch (RemoteException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				} catch (ProxyException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				} catch (EmptySaleExcpetion e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		btnVerDetalleE.setBounds(323, 244, 285, 28);
		btnVerDetalleE.setText("Ver Detalle e Imprimir Factura");
		
		error = new Label(shell, SWT.NONE);
		error.setBounds(10, 224, 285, 14);
		error.setText("");
		
		Button btnNewButton = new Button(shell, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				shell.dispose();
			}
		});
	
		btnNewButton.setBounds(323, 282, 285, 28);
		btnNewButton.setText("Cancelar");

	}
}
